package jc.malpractice.service;


import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import jc.malpractice.db.DbUtil;
import jc.malpractice.db.Help;

import java.sql.*;
import java.text.SimpleDateFormat;

public final class UserService {

    public static JSONObject login(String username, String password) {
        Connection conn = DbUtil.getConn();
        System.out.println(conn);
        if (conn == null) return null;

        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        JSONObject jo = new JSONObject();

        try {

            preparedStatement = conn.prepareStatement(
                    "select * from user" +
                            " where username=? and password=? ");
            preparedStatement.setString(1, username);
            preparedStatement.setString(2, password);
            resultSet = preparedStatement.executeQuery();

            jo = Help.Result3Json(resultSet);
//            System.out.println();
            return jo;
//            String sql = "select username, password ,role from user" +
//                            " where username=? and password=? ";
//             preparedStatement = conn.prepareStatement( sql);
//            preparedStatement.setString(1,username);
//            preparedStatement.setString(2,password);
//
//            resultSet = preparedStatement.executeQuery();
//            JSONObject json = Help.Result3Json(resultSet);
////            System.out.println();
//            return json;
        } catch (SQLException e) {
            try {
                //conn.rollback();
            } catch (Exception ee) {
                System.out.println(ee.getMessage());
            }

            System.out.println(e.getMessage());
        } finally {
            try {
                if (resultSet != null) resultSet.close();
                if (preparedStatement != null) preparedStatement.close();
                conn.close();
            } catch (SQLException e) {
                System.out.println(e.getMessage());
            }
        }

        jo.set("code", 1002);
        jo.set("msg", "登录失败");

        return jo;
    }

    public static JSONObject reg(String username, String password) {
        Connection conn = DbUtil.getConn();
        System.out.println(conn);
        if (conn == null) return null;

        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        JSONObject jo = new JSONObject();

        try {

            preparedStatement = conn.prepareStatement("insert into user (username,password,role) values (?,?,'user')");
            preparedStatement.setString(1, username);
            preparedStatement.setString(2, password);
            preparedStatement.executeUpdate();

            jo.set("code", 1000);
            jo.set("msg", "注册成功");
//            System.out.println();
            return jo;
//            String sql = "select username, password ,role from user" +
//                            " where username=? and password=? ";
//             preparedStatement = conn.prepareStatement( sql);
//            preparedStatement.setString(1,username);
//            preparedStatement.setString(2,password);
//
//            resultSet = preparedStatement.executeQuery();
//            JSONObject json = Help.Result3Json(resultSet);
////            System.out.println();
//            return json;
        } catch (SQLException e) {
            try {
                //conn.rollback();
            } catch (Exception ee) {
                System.out.println(ee.getMessage());
            }

            System.out.println(e.getMessage());
        } finally {
            try {
                if (resultSet != null) resultSet.close();
                if (preparedStatement != null) preparedStatement.close();
                conn.close();
            } catch (SQLException e) {
                System.out.println(e.getMessage());
            }
        }

        jo.set("code", 1002);
        jo.set("msg", "注册失败");

        return jo;
    }

    // 查询用户
    public static JSONObject selectUser(String role,String reporter) {
        Connection conn = DbUtil.getConn();
        System.out.println(conn);
        if (conn == null) return null;

        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        JSONObject jo = new JSONObject();

        try {

            if("admin".equals(role)) {
                preparedStatement = conn.prepareStatement("select * from user ");
            } else {
                preparedStatement = conn.prepareStatement(
                        "select * from user " +
                                "where id=? ");
                preparedStatement.setString(1, reporter);
            }
            resultSet = preparedStatement.executeQuery();

            jo = Help.Result3Json(resultSet);
            return jo;
        } catch (SQLException e) {
            try {
                //conn.rollback();
            } catch (Exception ee) {
                System.out.println(ee.getMessage());
            }

            System.out.println(e.getMessage());
        } finally {
            try {
                if (resultSet != null) resultSet.close();
                if (preparedStatement != null) preparedStatement.close();
                conn.close();
            } catch (SQLException e) {
                System.out.println(e.getMessage());
            }
        }

        jo.set("code", 1002);
        jo.set("msg", "查找失败");

        return jo;
    }

    //更新密码
    public static JSONObject updatePassword(String id,String password) {
        Connection conn = DbUtil.getConn();
        System.out.println(conn);
        if (conn == null) return null;

        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        JSONObject jo = new JSONObject();

        try {
            preparedStatement = conn.prepareStatement("update user set password=? where id=?");
            preparedStatement.setString(1, password);
            preparedStatement.setString(2, id);
            if(preparedStatement.executeUpdate()!=0) {
                jo.set("code", 1000);
                jo.set("msg", "修改成功");
                return jo;
            }
        } catch (SQLException e) {
            try {
//                conn.rollback();
            } catch (Exception ee) {
                System.out.println(ee.getMessage());
            }

            System.out.println(e.getMessage());
        } finally {
            try {
                if (resultSet != null) resultSet.close();
                if (preparedStatement != null) preparedStatement.close();
                conn.close();
            } catch (SQLException e) {
                System.out.println(e.getMessage());
            }
        }

        jo.set("code", 1002);
        jo.set("msg", "修改失败");

        return jo;
    }

    // 查找 管理员
    public static JSONObject sel_hander() {
        Connection conn = DbUtil.getConn();
        System.out.println(conn);
        if (conn == null) return null;

        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        JSONObject jo = new JSONObject();

        try {

            preparedStatement = conn.prepareStatement(
                    "select distinct *  from reporter ");

            resultSet = preparedStatement.executeQuery();

            jo = Help.Result2Json(resultSet);
            return jo;
        } catch (SQLException e) {
            try {
                //conn.rollback();
            } catch (Exception ee) {
                System.out.println(ee.getMessage());
            }

            System.out.println(e.getMessage());
        } finally {
            try {
                if (resultSet != null) resultSet.close();
                if (preparedStatement != null) preparedStatement.close();
                conn.close();
            } catch (SQLException e) {
                System.out.println(e.getMessage());
            }
        }


        return jo;
    }

    // 查找 管理员
    public static JSONObject selectHanderAll(String handler_name) {
        Connection conn = DbUtil.getConn();
        System.out.println(conn);
        if (conn == null) return null;

        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        JSONObject jo = new JSONObject();

        try {

            preparedStatement = conn.prepareStatement(
                    "select distinct *  from reporter where reporter_name = ?");
            preparedStatement.setString(1, handler_name);

            resultSet = preparedStatement.executeQuery();

            jo = Help.Result2Json(resultSet);
            return jo;
        } catch (SQLException e) {
            try {
                //conn.rollback();
            } catch (Exception ee) {
                System.out.println(ee.getMessage());
            }

            System.out.println(e.getMessage());
        } finally {
            try {
                if (resultSet != null) resultSet.close();
                if (preparedStatement != null) preparedStatement.close();
                conn.close();
            } catch (SQLException e) {
                System.out.println(e.getMessage());
            }
        }


        return jo;
    }

    // 查询所有用户
    public static JSONObject selectReporter() {
        Connection conn = DbUtil.getConn();
        System.out.println(conn);
        if (conn == null) return null;

        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        JSONObject jo = new JSONObject();

        try {
            preparedStatement = conn.prepareStatement("select * from reporter ");
            resultSet = preparedStatement.executeQuery();

            jo = Help.Result5Json(resultSet);
            return jo;
        } catch (SQLException e) {
            try {
                //conn.rollback();
            } catch (Exception ee) {
                System.out.println(ee.getMessage());
            }

            System.out.println(e.getMessage());
        } finally {
            try {
                if (resultSet != null) resultSet.close();
                if (preparedStatement != null) preparedStatement.close();
                conn.close();
            } catch (SQLException e) {
                System.out.println(e.getMessage());
            }
        }

        jo.set("code", 1002);
        jo.set("msg", "查找失败");

        return jo;
    }

    //更改权限
    public static JSONObject update_is_admin(String id,String str) {
        Connection conn = DbUtil.getConn();
        System.out.println(conn);
        if (conn == null) return null;

        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        JSONObject jo = new JSONObject();

        try {
            int is_admin = 0;
            if("是".equals(str)) {
                is_admin = 0;
            } else {
                is_admin = 1;
            }
            preparedStatement = conn.prepareStatement("update reporter set is_admin=? where id=?");
            preparedStatement.setInt(1, is_admin);
            preparedStatement.setString(2, id);
            if(preparedStatement.executeUpdate()!=0) {
                jo.set("code", 1000);
                jo.set("msg", "修改成功");
                return jo;
            }
        } catch (SQLException e) {
            try {
//                conn.rollback();
            } catch (Exception ee) {
                System.out.println(ee.getMessage());
            }

            System.out.println(e.getMessage());
        } finally {
            try {
                if (resultSet != null) resultSet.close();
                if (preparedStatement != null) preparedStatement.close();
                conn.close();
            } catch (SQLException e) {
                System.out.println(e.getMessage());
            }
        }

        jo.set("code", 1002);
        jo.set("msg", "修改失败");

        return jo;
    }

    public static void main(String[] args) {
        System.out.println(selectReporter());
    }

}
